package com.geovis.boot.common.util;

import com.geovis.boot.common.dto.ExcelLogDto;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 月
 * @Description //TODO $
 * @Date 11:14
 * @Param
 * @return
 * @Version 1.0
 */
public class WriteDocUtils {

    public void demoWrite() throws Exception {
        List<ExcelLogDto> list = new ArrayList<ExcelLogDto>();
        ExcelLogDto excelLogDto = new ExcelLogDto();
        excelLogDto.setId(1);
        excelLogDto.setAction("login");
        excelLogDto.setCreateTime(new Date());
        excelLogDto.setUserName("admin");
        list.add(excelLogDto);

        String templatePath = "D:\\template.doc";
        InputStream is = new FileInputStream(templatePath);
        OutputStream os = null;
        HWPFDocument doc = new HWPFDocument(is);
        Range range = doc.getRange();
        for(int i=0;i<list.size();i++){
            ExcelLogDto excelLog = list.get(i);
            range.replaceText("${name}", excelLog.getUserName());
            range.replaceText("${action}", excelLog.getAction());
            os = new FileOutputStream(new File("D:\\"+excelLog.getUserName()+".doc"));
            //把doc输出到输出流中
            doc.write(os);
        }
        os.close();
        is.close();
    }
}
